package com.spica.platform.websocket;

import com.spica.platform.websocket.interceptor.AuthHandshakeInterceptor;
import com.spica.platform.websocket.interceptor.SocketChannelInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@ConditionalOnClass(WebSocketMessage.class)
@EnableWebSocketMessageBroker
@ComponentScan
public class WebSocketAutoConfig implements WebSocketMessageBrokerConfigurer {

    @Autowired
    @Lazy
    private SocketChannelInterceptor socketChannelInterceptor;

    @Autowired
    @Lazy
    private AuthHandshakeInterceptor authHandshakeInterceptor;

//    @Bean
//    @ConditionalOnBean(ChannelInterceptor.class)
//    public SocketChannelInterceptor socketChanelInterceptor(  ) {
//        return new SocketChannelInterceptor();
//    }

    /**
     * 注册stomp的端点
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 客户端连接端点
        registry.addEndpoint("/ws")
                .setAllowedOriginPatterns("*")
                .addInterceptors(authHandshakeInterceptor)
                .withSockJS();
    }

    /**
     * 配置信息代理
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        //enableSimpleBroker是使用内存中介，不依赖第三方组件,表示客户端订阅地址的前缀信息，也就是客户端接收服务端消息的地址的前缀信息
        registry.enableSimpleBroker("/topic/", "/queue/");
        registry.setUserDestinationPrefix("/queue/");
        registry.setApplicationDestinationPrefixes("/app");
//enableStompBrokerRelay使用第三方中间件如RabbitMq
//    	rabbitmq配置
//        registry.enableStompBrokerRelay("/topic/","/queue/")
//                .setRelayHost("localhost")
//                .setRelayPort(61613)
//                .setClientLogin("guest")
//                .setClientPasscode("guest")
//                .setVirtualHost("/");
//        registry.setApplicationDestinationPrefixes("/app");
    }

    /**
     * 配置客户端入站通道拦截器，用于传递从WebSocket客户端接收到的消息
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(socketChannelInterceptor);
    }

    /**
     * 配置客户端出站通道拦截器，用于向WebSocket客户端发送服务器消息
     */
    @Override
    public void configureClientOutboundChannel(ChannelRegistration registration) {
       // registration.interceptors(socketChannelInterceptor);
    }
}
